
.LC0:
	.string "rb"

.LC1:
	.string "wb"

.LC2:
	.string "cannot open s_file.\n"

.LC3:
	.string "cannot open or create c_file.\n"

.LC4:
	.string "please input encode file name:\n"

.LC5:
	.string "please input Password:\n"

.LC6:
	.string "please input saved file name:\n"

.globl main


encrypt:
	pushq %rbp
	movq %rsp,%rbp
	subq $0x1000,%rsp

	movq %rdi,-0x1000(%rbp) /* s_file */
	movq %rsi,-0xff8(%rbp) /* pwd */
	movq %rdx,-0xff0(%rbp) /* c_file */
	movq $0,-0xfe8(%rbp) /* fp2 */
	movq $0,-0xfe0(%rbp) /* fp1 */
	movl $0,-0xfd0(%rbp) /* ch */
	movl $0,-0xfc0(%rbp) /* i */

	leaq .LC0(%rip),%rsi 
	movq -0x1000(%rbp),%rdi /* s_file */
	call fopen 
	movq %rax,-0xfe0(%rbp) /* fp1 */

	cmpq $0,-0xfe0(%rbp) /* fp1 */
	jne .Lencrypt_400

	leaq .LC2(%rip),%rdi 
	movl $0,%eax 
	call printf 

	movl $1,%edi
	call exit 

.Lencrypt_400:
	leaq .LC1(%rip),%rsi
	movq -0xff0(%rbp),%rdi /* c_file */
	call fopen 
	movq %rax,-0xfe8(%rbp) /* fp2 */
	cmpq $0,-0xfe8(%rbp) /* fp2 */
	jne .Lencrypt_380

	leaq .LC3(%rip),%rdi 
	movl $0,%eax 
	call printf 

	movl $1,%edi
	call exit 

.Lencrypt_380:
	movq -0xfe0(%rbp),%rdi /* fp1 */
	call fgetc 
	movb %al,-0xfd0(%rbp) /* ch */


	jmp .Lencrypt_300

.Lencrypt_370:
	movq -0xff8(%rbp),%rdi /* pwd */
	
	movl -0xfc0(%rbp),%eax /* i */
	movslq %eax,%rax
	addq %rax,%rdi 
	movb (%rdi),%al /* *(pwd+i) */
	xorb -0xfd0(%rbp),%al /* ch */
	movb %al,-0xfd0(%rbp) /* ch */

	addl $1,-0xfc0(%rbp) /* i */
	
	movq -0xfe8(%rbp),%rsi /* fp2 */
	movb -0xfd0(%rbp),%al /* ch */
	movsbl %al,%edi 
	call fputc 

	movq -0xfe0(%rbp),%rdi /* fp1 */
	call fgetc 
	movb %al,-0xfd0(%rbp) /* ch */ 

	cmpl $9,-0xfc0(%rbp) /* i */
	jle .Lencrypt_300

	movl $0,-0xfc0(%rbp) /* i */

.Lencrypt_300:
	movq -0xfe0(%rbp),%rdi /* fp1 */
	call feof 

	testl %eax,%eax
	je .Lencrypt_370

	movq -0xfe0(%rbp),%rdi /* fp1 */ 
	call fclose

	movq -0xfe8(%rbp),%rdi /* fp1 */ 
	call fclose

	movl $0,%eax
	leave
	ret

main:
	pushq %rbp
	movq %rsp,%rbp
	subq $0x1000,%rsp

	movl %edi,-0x1000(%rbp) /* argc */
	movq %rsi,-0xff8(%rbp) /* argv */
	movl $0,-0xff0(%rbp) /* sourcefile */
	movl $0,-0xfa0(%rbp) /* codefile */
	movl $0,-0xf50(%rbp) /* pwd */

	cmpl $4,-0x1000(%rbp) /* argc */
	je .Lmain_400

	leaq .LC4(%rip),%rdi
	movl $0,%eax 
	call printf 

	leaq -0xff0(%rbp),%rdi /* sourcefile */
	call gets 

	leaq .LC5(%rip),%rdi
	movl $0,%eax 
	call printf 

	leaq -0xf50(%rbp),%rdi /* pwd */
	call gets 

	leaq .LC6(%rip),%rdi
	movl $0,%eax 
	call printf 

	leaq -0xfa0(%rbp),%rdi /* codefile */
	call gets 

	leaq -0xfa0(%rbp),%rdx /* codefile */
	leaq -0xf50(%rbp),%rsi /* pwd */
	leaq -0xff0(%rbp),%rdi /* sourcefile */
	call encrypt 

	jmp .Lmain_0

.Lmain_400:

	movl $1,%eax
	sall $3,%eax
	movslq %eax,%rax 
	movq -0xff8(%rbp),%rdi  /* argv */
	movq 0(%rdi,%rax,1),%rsi /* argv[1] */
	leaq -0xff0(%rbp),%rdi /* sourcefile */
	call strcpy 

	movl $2,%eax
	sall $3,%eax
	movslq %eax,%rax 
	movq -0xff8(%rbp),%rdi  /* argv */
	movq 0(%rdi,%rax,1),%rsi /* argv[1] */
	leaq -0xf50(%rbp),%rdi /* pwd */
	call strcpy 

	movl $3,%eax
	sall $3,%eax
	movslq %eax,%rax 
	movq -0xff8(%rbp),%rdi  /* argv */
	movq 0(%rdi,%rax,1),%rsi /* argv[1] */
	leaq -0xfa0(%rbp),%rdi /* codefile */
	call strcpy 
	
	leaq -0xfa0(%rbp),%rdx /* codefile */
	leaq -0xf50(%rbp),%rsi /* pwd */
	leaq -0xff0(%rbp),%rdi /* sourcefile */
	call encrypt

.Lmain_0:
	movl $0,%eax
	leave
	ret
